<?php

class Menu_order_ajax extends Admin_Controller {

	function __construct()
	{
		parent::__construct();	
		$this->sSkinFile = 'ajax';
	}
	
	public function index()
	{
		return null;
	}
	
	
	
	public function save_menu_order()
	{
		$aLevels = $this->input->post('hiered', true, array());
		
		$this->load->model('db/page_parent_lookup', 'page_parent_lookup');
		
		$this->save_menu_order_level(NULL, $aLevels);
	}
	
	private function save_menu_order_level($iParentId = NULL, $aLevels = array())
	{
		foreach($aLevels as $iPosition => $aLevel)
		{
			if(isset($aLevel['id']))
			{
				$iPageId = $aLevel['id'];
				
				if($iParentId !== NULL)
				{
					$this->page_parent_lookup->page_id = $iPageId;
					
					$qPage = $this->page_parent_lookup->get(1);
					
					if($qPage->num_rows() === 1)
					{
						$this->page_parent_lookup->lookup_id = $qPage->row(0)->lookup_id;
						$this->page_parent_lookup->page_parent_id = $iParentId;
						$this->page_parent_lookup->page_pos = $iPosition;
						
						$this->page_parent_lookup->save();
					}
					
					$this->page_parent_lookup->clear();
				}
				
				if(isset($aLevel['children']))
				{
					$this->save_menu_order_level($iPageId, $aLevel['children']);
				}
			}
		}
	}
	
}

?>